package com.vodafone.mapreduce.divide;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import org.apache.log4j.Logger;

import com.vodafone.mapreduce.MapReduce;

/**
 * TODO: Insert your comments here!
 */
public class DivideProcess extends Thread {

    public static Logger log = MapReduce.LOG;

    public static void main(String[] args) {
        DivideConfig.ClientConfig config = DivideConfig.ClientConfig.fromKeyValueCmdlineArgs(args);
        log.info("Config:");
        log.info(config.toString());
        DivideProcess p = new DivideProcess(config);
        p.start();
    }

    private final DivideConfig.ClientConfig config;

    DivideProcess(DivideConfig.ClientConfig config) {
        this.config = config;
    }

    public void run() {
        LineNumberReader r = null;
        try {
            r = new LineNumberReader(new FileReader(config.inputFilename));
        } catch (FileNotFoundException ex) {
            log.fatal("Specified input file does not exist or cannot be accessed: " + ex.getMessage(), ex);
        }
        String line = null;
        do {
            try {
                line = r.readLine();
                if (line != null) {
                    log.info("Line #" + r.getLineNumber() + ": " + line);
                }
            } catch (IOException ex) {
                log.fatal("Could not read next line from file: " + ex.getMessage(), ex);
            }
        } while (line != null);
    }

}
